package com.imooc.distributedemo.resume.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * leetcode287  寻找数组中重复数
 */
public class Solution287 {

    public static void main(String[] args) {
//        int[] nums = {3,1,3,4,2};
        int[] nums = {1,3,4,2,2};
        int result = findDuplicate(nums);
        System.out.println(result);
    }

    public static int findDuplicate(int[] nums) {
        if(null==nums||nums.length<1){
            return  -1;
        }
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if(set.contains(nums[i])){
                return nums[i];
            }else{
                set.add(nums[i]);
            }
        }
        return -1;
    }
}
